package com.by.filter;


import com.by.config.WhiteUrlsConfig;
import com.by.constant.AuthConstants;
import com.by.constant.BusinessEnum;
import com.by.constant.HttpConstants;
import com.by.model.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.lang.annotation.Annotation;
import java.util.Date;

/**
 * @author: Fsw
 * @com.by.filter
 * @date 2025-04-30 11:19
 * 全局Token过滤器
 */
@Component
@Slf4j
public class AuthFilter implements GlobalFilter, Order {

    @Autowired
    private WhiteUrlsConfig whiteUrlsConfig;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 校验Token
     * 1.获取请求路径
     * 2.判断请求路径是否可放行
     *    放行：不验证身份
     *    不放行：需要对其进行身份的认证
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //获取请求路径
        String  path = request.getPath().toString();
        //判断当前请求路径是否可放行 ,是否存在于白名单中
        if (whiteUrlsConfig.getAllowUrls().contains(path)){
            //包含：即请求路径包含在白名单中，即需要放行
            return chain.filter(exchange);
        }
        //请求路径不包含，需要对身份进行验证
        // 从约定好的位置获取Authorization的值，值的格式为：bearer token
        String authorizationValue = request.getHeaders().getFirst(AuthConstants.AUTHORIZATION);
        //判断是否有值
        if(StringUtils.hasText(authorizationValue)){
            //从Authorization的值中获取token
            String tokenValue = authorizationValue.replaceFirst(AuthConstants.BEARER, "");
            //判断token值是否有值且是否在Redis中存在
            if(StringUtils.hasText(tokenValue)&& stringRedisTemplate.hasKey(AuthConstants.LOGIN_TOKEN_PREFIX+tokenValue)){
                //身份验证通过，放行
                return chain.filter(exchange);
            }
        }

        //流程如果走到这，身份验证不合法或没有通过
        log.error("拦截非法请求，时间:{},请求API路径:{}",new Date(),path);
        //获取相应对象
        ServerHttpResponse response = exchange.getResponse();
        //设置响应头信息
        response.getHeaders().set(HttpConstants.CONTENT_TYPE,HttpConstants.APPLICATION_JSON);
        //设置响应消息
        Result<Object> result = Result.fail(BusinessEnum.UN_AUTHORIZATION);
        //创建一个ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        byte[] bytes = new byte[0];
        try {
             bytes = objectMapper.writeValueAsBytes(result);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
        return response.writeWith(Mono.just(dataBuffer));
    }

    @Override
    public int value() {
        return -5;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return null;
    }
}

